/**
  * 排行榜相关数据的store
  * @author: Ruan Jiazhen
  * @date: 2024-04-29 10:27:48
* */
// XXX 已弃用，考虑删除

import { defineStore } from 'pinia';
import { ref } from 'vue';
import { requestInfluenceRank, requestRedPacketRank } from '@/api/api-rank';
import { randomId } from '@/utils/tools';

export const useRank = defineStore('rank', () => {
  // 红包流水排行榜中的个人信息
  const personalRedPacketRank = ref<PersonalRedPacketRankItem>({
    name: '',
    avatar: '',
    totalTurnover: 100,
    difference: 10000,
    rank: '99+',
  });

  // 红包流水榜
  const redPacketRank = ref<RedPacketRankItem[]>([]);

  // 获取红包流水排行榜
  const getRedPacketRank = async () => {
    const res = await requestRedPacketRank();
    const resData: RankRes = res.data;
    console.log('红包流水排行榜', resData);
    redPacketRank.value = resData?.data?.list.map((item, index) => ({
      id: randomId(),
      rank: index + 1,
      name: item.emcee_name,
      avatar: item.avator,
      reward: item.awardName,
    })) ?? [];
    console.log('红包流水榜', redPacketRank.value);
    // 个人信息
    personalRedPacketRank.value = {
      name: resData?.data?.myInfo.emcee_name,
      avatar: resData?.data?.myInfo.avator,
      totalTurnover: resData?.data?.myInfo.rank_hby,
      difference: resData?.data?.distance_hby,
      rank: resData?.data?.myRank,
    };
    console.log('个人信息', personalRedPacketRank.value);
  };

  // 红包流水榜页面当前弹窗类型
  const redPacketRankCurrentModalType = ref<RankModalType>('no');

  // 修改红包流水榜页面当前弹窗类型
  const changeRedPacketRankCurrentModalType = (type: RankModalType) => {
    redPacketRankCurrentModalType.value = type;
  };

  // 红包流水榜奖励
  const redPacketRankReward = ref<PrizeItem[]>([
    {
      id: '1',
      name: 'iphone 14',
      desc: '第一名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-1.png',
    },
    {
      id: '2',
      name: 'iphone 13',
      desc: '第二名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-2.png',
    },
    {
      id: '3',
      name: 'iphone 12',
      desc: '第三名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-3.png',
    },
    {
      id: '4',
      name: 'iphone 11',
      desc: '第四名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-4.png',
    },
    {
      id: '5',
      name: 'iphone 10',
      desc: '第五名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-5.png',
    },
    {
      id: '6',
      name: 'iphone 9',
      desc: '第6~20名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-6.png',
    },
    {
      id: '7',
      name: 'iphone 8',
      desc: '第21~50名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/red-packet-reward-7.png',
    },
  ]);

  // 影响力排行榜中的个人信息
  const personalInfluenceRank = ref<PersonalInfluenceRankItem>({
    name: '',
    avatar: '',
    inviteNum: 3,
    difference: 50,
    rank: 1000,
  });

  // 影响力排行榜
  const influenceRank = ref<InfluenceRankItem[]>([]);

  // 获取影响力排行榜
  const getInfluenceRank = async () => {
    const res = await requestInfluenceRank();
    const resData: RankRes = res.data;
    influenceRank.value = resData?.data?.list.map((item, index) => ({
      id: randomId(),
      rank: index + 1,
      name: item.emcee_name,
      avatar: item.avator,
      inviteNum: item.rank_invite,
      reward: item.awardName,
    })) ?? [];
    console.log('影响力排行榜', influenceRank.value);

    // 个人信息
    personalInfluenceRank.value = {
      name: resData?.data?.myInfo.emcee_name, // 姓名
      avatar: resData?.data?.myInfo.avator, // 头像
      inviteNum: resData?.data?.myInfo.rank_invite, // 邀请人数
      difference: resData?.data?.distance_hby, // 距离上一名的差值
      rank: resData?.data?.myRank, // 排名
    };
    console.log('个人信息', personalInfluenceRank.value);
  };

  // 影响力排行榜当前弹窗类型
  const influenceRankCurrentModalType = ref<RankModalType>('no');

  // 修改影响力排行榜当前弹窗类型
  const changeInfluenceRankCurrentModalType = (type: RankModalType) => {
    influenceRankCurrentModalType.value = type;
  };

  // 影响力排行榜奖励
  const influenceRankReward = ref<PrizeItem[]>([
    {
      id: '1',
      name: 'iphone 14',
      desc: '第一名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-1.png',
    },
    {
      id: '2',
      name: 'iphone 13',
      desc: '第二名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-2.png',
    },
    {
      id: '3',
      name: 'iphone 12',
      desc: '第三名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-3.png',
    },
    {
      id: '4',
      name: 'iphone 11',
      desc: '第四名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-4.png',
    },
    {
      id: '5',
      name: 'iphone 10',
      desc: '第五名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-5.png',
    },
    {
      id: '6',
      name: 'iphone 9',
      desc: '第6~7名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-6.png',
    },
    {
      id: '7',
      name: 'iphone 8',
      desc: '第8~10名',
      img: 'https://ustatic.hudongmiao.com/joymew-host-contest/influence-rank-reward-7.png',
    },
  ]);

  //
  return {
    personalRedPacketRank,
    redPacketRank,
    getRedPacketRank,
    redPacketRankCurrentModalType,
    changeRedPacketRankCurrentModalType,
    redPacketRankReward,
    personalInfluenceRank,
    influenceRank,
    getInfluenceRank,
    influenceRankCurrentModalType,
    changeInfluenceRankCurrentModalType,
    influenceRankReward,
  };
});
